#include "backend.h"
#include "Backend/visualrobot.h"

Backend::Backend(){
    this->BackendInit();
}

Backend::~Backend(){
    this->BackendDeInit();
}
void Backend::FastTimerLoop(){

}
void Backend::SlowTimerLoop(){

}

void Backend:: BackendInit(){
    this->stFnBtn.Fn1 = false;
    this->stFnBtn.Fn2 = false;
    this->stFnBtn.Fn3 = false;
    this->stFnBtn.Fn4 = false;

    this->stDirectionBtn.XN = false;
    this->stDirectionBtn.XP = false;
    this->stDirectionBtn.YN = false;
    this->stDirectionBtn.YP = false;
    this->stDirectionBtn.ZN = false;
    this->stDirectionBtn.ZP = false;

    this->stDirectionBtn.AN = false;
    this->stDirectionBtn.AP = false;
    this->stDirectionBtn.BN = false;
    this->stDirectionBtn.BP = false;
    this->stDirectionBtn.CN = false;
    this->stDirectionBtn.CP = false;

    this->stDirectionBtn.E1N = false;
    this->stDirectionBtn.E1P = false;
    this->stDirectionBtn.E2N = false;
    this->stDirectionBtn.E2P = false;

    this->stSystemBtn.vPower = false;
    this->stSystemBtn.Pause = false;
    this->stSystemBtn.Run = false;
    this->stSystemBtn.Stop = false;
    this->stSystemBtn.EStop = false;

    this->stOperationInfo.ToolNumber = 0;
    this->stOperationInfo.WorkNumber = 0;
    this->stOperationInfo.MannualOvn = 100;
    this->stOperationInfo.ProgramOvn = 100;

    this->stOperationInfo.MotionCoordinate = 1; //默认在机器人足部坐标系
    this->stOperationInfo.OperationMode = 0;

    this->pvisualRobot = new visualRobot();
}

void Backend :: BackendDeInit(){
    delete this->pvisualRobot;
}

bool Backend :: onbtnclicked_vPower(){
    if(this->stSystemBtn.vPower == false) this->stSystemBtn.vPower = true;
    else                                  this->stSystemBtn.vPower = false;
    return this->stSystemBtn.vPower;
}
bool Backend :: onbtnclicked_ESTOP_Clicked(){
    if(this->stSystemBtn.EStop == false) this->stSystemBtn.EStop = true;
    else                                 this->stSystemBtn.EStop = false;
    return this->stSystemBtn.EStop;
}

bool Backend :: onbtnclicked_Pause(){
    if(this->stSystemBtn.Pause == false) this->stSystemBtn.Pause = true;
    else                                 this->stSystemBtn.Pause = false;
    return this->stSystemBtn.Pause;
}
bool Backend :: onbtnclicked_Run(){
    if(this->stSystemBtn.Run == false) this->stSystemBtn.Run = true;
    else                               this->stSystemBtn.Run = false;
    return this->stSystemBtn.Run;
}
bool Backend :: onbtnclicked_Stop(){
    if(this->stSystemBtn.Stop == false) this->stSystemBtn.Stop = true;
    else                                this->stSystemBtn.Stop = false;
    return this->stSystemBtn.Stop;
}

void Backend :: onbtnclicked_fn1(){
    if(this->stFnBtn.Fn1 == false) this->stFnBtn.Fn1 = true;
    else                           this->stFnBtn.Fn1 = false;
}
void Backend :: onbtnclicked_fn2(){
    if(this->stFnBtn.Fn2 == false) this->stFnBtn.Fn2 = true;
    else                           this->stFnBtn.Fn2 = false;
}
void Backend :: onbtnclicked_fn3(){
    if(this->stFnBtn.Fn3 == false) this->stFnBtn.Fn3 = true;
    else                           this->stFnBtn.Fn3 = false;
}
void Backend :: onbtnclicked_fn4(){
    if(this->stFnBtn.Fn4 == false) this->stFnBtn.Fn4 = true;
    else                           this->stFnBtn.Fn4 = false;
}

void Backend :: onbtnpressed_XP(){
    this->stDirectionBtn.XP = true;
}
void Backend :: onbtnpressed_YP(){
    this->stDirectionBtn.YP = true;
}
void Backend :: onbtnpressed_ZP(){
    this->stDirectionBtn.ZP = true;
}
void Backend :: onbtnpressed_AP(){
    this->stDirectionBtn.AP = true;
}
void Backend :: onbtnpressed_BP(){
    this->stDirectionBtn.BP = true;
}
void Backend :: onbtnpressed_CP(){
    this->stDirectionBtn.CP = true;
}

void Backend :: onbtnpressed_XN(){
    this->stDirectionBtn.XN = true;
}
void Backend :: onbtnpressed_YN(){
    this->stDirectionBtn.YN = true;
}
void Backend :: onbtnpressed_ZN(){
    this->stDirectionBtn.ZN = true;
}
void Backend :: onbtnpressed_AN(){
    this->stDirectionBtn.AN = true;
}
void Backend :: onbtnpressed_BN(){
    this->stDirectionBtn.BN = true;
}
void Backend :: onbtnpressed_CN(){
    this->stDirectionBtn.CN = true;
}

void Backend :: onbtnpressed_E1P(){
    this->stDirectionBtn.E1P = true;
}
void Backend :: onbtnpressed_E2P(){
    this->stDirectionBtn.E2P = true;
}
void Backend :: onbtnpressed_E1N(){
    this->stDirectionBtn.E1N = true;
}
void Backend :: onbtnpressed_E2N(){
    this->stDirectionBtn.E2N = true;
}

void Backend :: onbtnreleased_XP(){
    this->stDirectionBtn.XP = false;
}
void Backend :: onbtnreleased_YP(){
    this->stDirectionBtn.YP = false;
}
void Backend :: onbtnreleased_ZP(){
    this->stDirectionBtn.ZP = false;
}
void Backend :: onbtnreleased_AP(){
    this->stDirectionBtn.AP = false;
}
void Backend :: onbtnreleased_BP(){
    this->stDirectionBtn.BP = false;
}
void Backend :: onbtnreleased_CP(){
    this->stDirectionBtn.CP = false;
}

void Backend :: onbtnreleased_XN(){
    this->stDirectionBtn.XN = false;
}
void Backend :: onbtnreleased_YN(){
    this->stDirectionBtn.YN = false;
}
void Backend :: onbtnreleased_ZN(){
    this->stDirectionBtn.ZN = false;
}
void Backend :: onbtnreleased_AN(){
    this->stDirectionBtn.AN = false;
}
void Backend :: onbtnreleased_BN(){
    this->stDirectionBtn.BN = false;
}
void Backend :: onbtnreleased_CN(){
    this->stDirectionBtn.CN = false;
}

void Backend :: onbtnreleased_E1P(){
    this->stDirectionBtn.E1P = false;
}
void Backend :: onbtnreleased_E2P(){
    this->stDirectionBtn.E2P = false;
}
void Backend :: onbtnreleased_E1N(){
    this->stDirectionBtn.E1N = false;
}
void Backend :: onbtnreleased_E2N(){
    this->stDirectionBtn.E2N = false;
}

void Backend :: onbtnclicked_MannualOverrideP(double& value){
    if     (this->stOperationInfo.MannualOvn == 0)    this->stOperationInfo.MannualOvn = 0.01;
    else if(this->stOperationInfo.MannualOvn == 0.01) this->stOperationInfo.MannualOvn = 0.1;
    else if(this->stOperationInfo.MannualOvn == 0.1)  this->stOperationInfo.MannualOvn = 0.2;
    else if(this->stOperationInfo.MannualOvn == 0.2)  this->stOperationInfo.MannualOvn = 0.5;
    else if(this->stOperationInfo.MannualOvn == 0.5)  this->stOperationInfo.MannualOvn = 1;
    else if(this->stOperationInfo.MannualOvn == 1)    this->stOperationInfo.MannualOvn = 2;
    else if(this->stOperationInfo.MannualOvn == 2)    this->stOperationInfo.MannualOvn = 5;
    else if(this->stOperationInfo.MannualOvn == 5)    this->stOperationInfo.MannualOvn = 10;
    else if(this->stOperationInfo.MannualOvn == 10)   this->stOperationInfo.MannualOvn = 30;
    else if(this->stOperationInfo.MannualOvn == 30)   this->stOperationInfo.MannualOvn = 50;
    else if(this->stOperationInfo.MannualOvn == 50)   this->stOperationInfo.MannualOvn = 80;
    else if(this->stOperationInfo.MannualOvn == 80)   this->stOperationInfo.MannualOvn = 100;
    else if(this->stOperationInfo.MannualOvn == 100)  this->stOperationInfo.MannualOvn = 100;

    value = this->stOperationInfo.MannualOvn;
}

void Backend :: onbtnclicked_ProgramOverrideP(double& value){
    if     (this->stOperationInfo.ProgramOvn == 0)    this->stOperationInfo.ProgramOvn = 0.01;
    else if(this->stOperationInfo.ProgramOvn == 0.01) this->stOperationInfo.ProgramOvn = 0.1;
    else if(this->stOperationInfo.ProgramOvn == 0.1)  this->stOperationInfo.ProgramOvn = 0.2;
    else if(this->stOperationInfo.ProgramOvn == 0.2)  this->stOperationInfo.ProgramOvn = 0.5;
    else if(this->stOperationInfo.ProgramOvn == 0.5)  this->stOperationInfo.ProgramOvn = 1;
    else if(this->stOperationInfo.ProgramOvn == 1)    this->stOperationInfo.ProgramOvn = 2;
    else if(this->stOperationInfo.ProgramOvn == 2)    this->stOperationInfo.ProgramOvn = 5;
    else if(this->stOperationInfo.ProgramOvn == 5)    this->stOperationInfo.ProgramOvn = 10;
    else if(this->stOperationInfo.ProgramOvn == 10)   this->stOperationInfo.ProgramOvn = 30;
    else if(this->stOperationInfo.ProgramOvn == 30)   this->stOperationInfo.ProgramOvn = 50;
    else if(this->stOperationInfo.ProgramOvn == 50)   this->stOperationInfo.ProgramOvn = 80;
    else if(this->stOperationInfo.ProgramOvn == 80)   this->stOperationInfo.ProgramOvn = 100;
    else if(this->stOperationInfo.ProgramOvn == 100)  this->stOperationInfo.ProgramOvn = 100;

    value = this->stOperationInfo.ProgramOvn;
}

void Backend :: onbtnclicked_MannualOverrideN(double& value){
    if     (this->stOperationInfo.MannualOvn == 0)    this->stOperationInfo.MannualOvn = 0;
    else if(this->stOperationInfo.MannualOvn == 0.01) this->stOperationInfo.MannualOvn = 0;
    else if(this->stOperationInfo.MannualOvn == 0.1)  this->stOperationInfo.MannualOvn = 0.01;
    else if(this->stOperationInfo.MannualOvn == 0.2)  this->stOperationInfo.MannualOvn = 0.1;
    else if(this->stOperationInfo.MannualOvn == 0.5)  this->stOperationInfo.MannualOvn = 0.2;
    else if(this->stOperationInfo.MannualOvn == 1)    this->stOperationInfo.MannualOvn = 0.5;
    else if(this->stOperationInfo.MannualOvn == 2)    this->stOperationInfo.MannualOvn = 1;
    else if(this->stOperationInfo.MannualOvn == 5)    this->stOperationInfo.MannualOvn = 2;
    else if(this->stOperationInfo.MannualOvn == 10)   this->stOperationInfo.MannualOvn = 5;
    else if(this->stOperationInfo.MannualOvn == 30)   this->stOperationInfo.MannualOvn = 10;
    else if(this->stOperationInfo.MannualOvn == 50)   this->stOperationInfo.MannualOvn = 30;
    else if(this->stOperationInfo.MannualOvn == 80)   this->stOperationInfo.MannualOvn = 50;
    else if(this->stOperationInfo.MannualOvn == 100)  this->stOperationInfo.MannualOvn = 80;

    value = this->stOperationInfo.MannualOvn;
}

void Backend :: onbtnclicked_ProgramOverrideN(double& value){
    if     (this->stOperationInfo.ProgramOvn == 0)    this->stOperationInfo.ProgramOvn = 0;
    else if(this->stOperationInfo.ProgramOvn == 0.01) this->stOperationInfo.ProgramOvn = 0;
    else if(this->stOperationInfo.ProgramOvn == 0.1)  this->stOperationInfo.ProgramOvn = 0.01;
    else if(this->stOperationInfo.ProgramOvn == 0.2)  this->stOperationInfo.ProgramOvn = 0.1;
    else if(this->stOperationInfo.ProgramOvn == 0.5)  this->stOperationInfo.ProgramOvn = 0.2;
    else if(this->stOperationInfo.ProgramOvn == 1)    this->stOperationInfo.ProgramOvn = 0.5;
    else if(this->stOperationInfo.ProgramOvn == 2)    this->stOperationInfo.ProgramOvn = 1;
    else if(this->stOperationInfo.ProgramOvn == 5)    this->stOperationInfo.ProgramOvn = 2;
    else if(this->stOperationInfo.ProgramOvn == 10)   this->stOperationInfo.ProgramOvn = 5;
    else if(this->stOperationInfo.ProgramOvn == 30)   this->stOperationInfo.ProgramOvn = 10;
    else if(this->stOperationInfo.ProgramOvn == 50)   this->stOperationInfo.ProgramOvn = 30;
    else if(this->stOperationInfo.ProgramOvn == 80)   this->stOperationInfo.ProgramOvn = 50;
    else if(this->stOperationInfo.ProgramOvn == 100)  this->stOperationInfo.ProgramOvn = 80;

    value = this->stOperationInfo.ProgramOvn;
}

